/**
 * Copyright(c) 2015 Jade Techonologies Co., Ltd.
 *
 * History:
 *   15-1-7 上午10:08 Created by lyyang
 */
package com.jade.framework.web.resolver;

import org.apache.commons.lang3.StringUtils;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.subject.Subject;
import org.springframework.core.MethodParameter;
import org.springframework.web.bind.support.WebArgumentResolver;
import org.springframework.web.context.request.NativeWebRequest;
import org.springframework.web.context.request.RequestAttributes;

/**
 * 解析controller中Subject参数
 *
 * @author <a href="mailto:lyyang@jade.com">lyyang</a>
 * @version 1.0 15-1-7 上午10:08
 */
public class SubjectArgumentsResolver implements WebArgumentResolver
{

    protected String token = "token";

    public void setToken(String token)
    {
        this.token = token;
    }

    public Object resolveArgument(MethodParameter methodParameter, NativeWebRequest nativeWebRequest)
            throws Exception
    {
        if (methodParameter.getParameterType().equals(Subject.class)) {
            String sid = nativeWebRequest.getParameter(token);
            if (!StringUtils.isEmpty(sid)){
                 return new Subject.Builder().sessionId(sid).buildSubject();
            }
            return SecurityUtils.getSubject();
        }
        return UNRESOLVED;
    }
}
